package com.example.hello.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.hello.entity.WorkOrder;
import com.example.hello.service.WorkOrderService;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/api/work-orders")
public class WorkOrderController {
    private final WorkOrderService workOrderService;

    public WorkOrderController(WorkOrderService workOrderService) {
        this.workOrderService = workOrderService;
    }

    @PostMapping
    public ResponseEntity<Map<String, Object>> createWorkOrder(@RequestBody WorkOrder workOrder) {
        WorkOrder created = workOrderService.createWorkOrder(workOrder);
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "工单创建成功");
        response.put("data", created);
        return new ResponseEntity<>(response, HttpStatus.CREATED);
    }

    @PutMapping("/{workOrderId}")
    public ResponseEntity<Map<String, Object>> updateWorkOrderStatus(
            @PathVariable Integer workOrderId,
            @RequestBody Map<String, String> request) {
        WorkOrder updated = workOrderService.updateWorkOrderStatus(workOrderId, request.get("status"));
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "工单状态更新成功");
        response.put("data", updated);
        return ResponseEntity.ok(response);
    }

    @GetMapping
    public ResponseEntity<Map<String, Object>> getWorkOrders(
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<WorkOrder> pageInfo = workOrderService.getWorkOrders(status, pageNum, pageSize);
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("data", pageInfo);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/{workOrderId}")
    public ResponseEntity<Map<String, Object>> getWorkOrderDetail(@PathVariable Integer workOrderId) {
        WorkOrder workOrder = workOrderService.getWorkOrderDetail(workOrderId);
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("data", workOrder);
        return ResponseEntity.ok(response);
    }
} 